package com.ruoyi.web.controller.qif;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.qif.domain.QifRoom;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qif.domain.QifExpenditure;
import com.ruoyi.qif.service.IQifExpenditureService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 锜风公寓-公账支出Controller
 * 
 * @author David
 * @date 2025-11-03
 */
@Controller
@RequestMapping("/qif/expenditure")
public class QifExpenditureController extends BaseController
{
    private String prefix = "qif/expenditure";

    @Autowired
    private IQifExpenditureService qifExpenditureService;

    @RequiresPermissions("qif:expenditure:view")
    @GetMapping()
    public String expenditure()
    {
        return prefix + "/expenditure";
    }

    /**
     * 查询锜风公寓-公账支出列表
     */
    @RequiresPermissions("qif:expenditure:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QifExpenditure qifExpenditure)
    {
        startPage();
        List<QifExpenditure> list = qifExpenditureService.selectQifExpenditureList(qifExpenditure);
        return getDataTable(list);
    }

    @RequiresPermissions("qif:expenditure:import")
    @Log(title = "锜风公寓-公账支出导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        try {
            ExcelUtil<QifExpenditure> util = new ExcelUtil<>(QifExpenditure.class);
            // 直接调用 importExcel 方法，指定工作表名称为'公账支出'
            List<QifExpenditure> qifExpenditureList = util.importExcel("公账支出", file.getInputStream(), 0);
            String message = qifExpenditureService.importQifExpenditure(qifExpenditureList, updateSupport);
            return AjaxResult.success(message);
        } catch (IOException e) {
            error("导入文件失败:"+ e.getMessage());
            return AjaxResult.error("导入文件失败: " + e.getMessage());
        } catch (Exception e) {
            error("处理导入数据失败: "+ e.getMessage());
            return AjaxResult.error("处理导入数据失败: " + e.getMessage());
        }
    }

    /**
     * 导出锜风公寓-公账支出列表
     */
    @RequiresPermissions("qif:expenditure:export")
    @Log(title = "锜风公寓-公账支出导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QifExpenditure qifExpenditure)
    {
        List<QifExpenditure> list = qifExpenditureService.selectQifExpenditureList(qifExpenditure);
        ExcelUtil<QifExpenditure> util = new ExcelUtil<QifExpenditure>(QifExpenditure.class);
        return util.exportExcel(list, "锜风公寓-公账支出数据");
    }

    /**
     * 新增锜风公寓-公账支出
     */
    @RequiresPermissions("qif:expenditure:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存锜风公寓-公账支出
     */
    @RequiresPermissions("qif:expenditure:add")
    @Log(title = "锜风公寓-公账支出新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QifExpenditure qifExpenditure)
    {
        return toAjax(qifExpenditureService.insertQifExpenditure(qifExpenditure));
    }

    /**
     * 修改锜风公寓-公账支出
     */
    @RequiresPermissions("qif:expenditure:edit")
    @GetMapping("/edit/{expenditureId}")
    public String edit(@PathVariable("expenditureId") Long expenditureId, ModelMap mmap)
    {
        QifExpenditure qifExpenditure = qifExpenditureService.selectQifExpenditureByExpenditureId(expenditureId);
        mmap.put("qifExpenditure", qifExpenditure);
        return prefix + "/edit";
    }

    /**
     * 修改保存锜风公寓-公账支出
     */
    @RequiresPermissions("qif:expenditure:edit")
    @Log(title = "锜风公寓-公账支出修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QifExpenditure qifExpenditure)
    {
        return toAjax(qifExpenditureService.updateQifExpenditure(qifExpenditure));
    }

    /**
     * 删除锜风公寓-公账支出
     */
    @RequiresPermissions("qif:expenditure:remove")
    @Log(title = "锜风公寓-公账支出删除", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(qifExpenditureService.deleteQifExpenditureByExpenditureIds(ids));
    }
}
